package com.bnz.service;

import com.bnz.entity.Student;
import com.bnz.entity.StudentExample;
import com.bnz.entity.vo.StudentVo;
import com.bnz.dao.ClassesMapper;
import com.bnz.dao.StudentMapper;
import com.bnz.service.StudentService;
import com.bnz.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service // 放入spring容器中
public class StudentServiceImpl implements StudentService {
    // 引入StudentMapper
    @Autowired
    private StudentMapper studentMapper;

    // 引入ClassesMapper
    @Autowired
    private ClassesMapper classesMapper;

    // 1、查询所有数据
    @Override
    public List<Student> findALL() {
        // 1.1、将学生信息全部查出
        List<Student> list = studentMapper.selectByExample(null);// 条件为空就是查询全部
        for (Student student : list) {
            // 1.2、设置学生的班级(用classesMapper的主键查询,然后锁定班级,再进行设置)
            String cname = classesMapper.selectByPrimaryKey(student.getCid()).getCname();
            student.setCname(cname);
        }
        return list;
    }

    // 2、分页查询数据
    @Override
    public PageResult<Student> findByPage(int page, int pagesize) {
        // 2.1、开始进项分页
        PageHelper.startPage(page,pagesize);
        // 2.2、开始进行数据查询
        StudentExample example = new StudentExample();
        List<Student> list = studentMapper.selectByExample(example);
        for (Student student : list) {
            // 1.2、设置学生的班级(用classesMapper的主键查询,然后锁定班级,再进行设置)
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        // 2.3、将查到数据的对象,包装为PageInfo
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        System.out.println(list);
        // 2.4、返回数据
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    // 3、条件查询
    @Override
    public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
        // 3.1、开始分页
        PageHelper.startPage(page,pagesize);
        // 3.2、开始查询
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(vo.getSname())){
            // like "%..%"
            criteria.andSnameLike("%" + vo.getSname() + "%");
        }
        if(!StringUtils.isEmpty(vo.getAddr())){
            criteria.andAddrLike("%" + vo.getAddr() + "%");
        }
        if(vo.getCid() != 0){
            criteria.andCidEqualTo(vo.getCid());
        }
        List<Student> students = studentMapper.selectByExample(example);
        // 3.3、班级名称
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        // 3.4、将对象包装为PageInfo
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        // 3.5、返回
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    // 4、添加
    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    // 5、修改
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    // 6、删除
    @Override
    public void delete(List<Integer> sids) {
        // 6.1、定义一个实例查询
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        // 6.2、查询条件（根据sid）
        criteria.andSidIn(sids);
        // 6.3、拿到上述条件，进行删除
        studentMapper.deleteByExample(example);
    }
}
